package com.gome.datasource;

import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import com.gome.util.TraceInfo;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class MongoDBDataSource {

	@Autowired
	private Mongo mongo;

	private DB init() {
		DB dbm = null;
		Collection<DB> dbs = null;
		try {
			dbs = mongo.getUsedDatabases();
		} catch (Exception e) {
			TraceInfo.traceError(MongoDBDataSource.class, e);
		}
		for (DB db : dbs) {
			dbm = db;
		}
		return dbm;
	}

	public DBCollection initDBCollection() {

		DBCollection coll = this.init().getCollection("rb");
		if (coll == null) {
			DBObject rb = new BasicDBObject();
			coll = this.init().createCollection("rb", rb);
		}
		return coll;
	}

	public Mongo getMongo() {
		return mongo;
	}

	public void setMongo(Mongo mongo) {
		this.mongo = mongo;
	}

}
